

let BaseView = require('./BaseView');
let setting = require('../settings');

let SudoKu = require('sudok');

const templateDiv = `
    <button id="btn_cal">计算</button><br/>
    key:<input type="text" id="word_key"><br/>
    <button id="btn_export">导出</button><br/>
    <button id="btn_clean">清空</button>
`;

class EditorView extends BaseView{
    constructor(){
        super();

        window.editor = this;
        this._gameApp  = G.gameMap;

        this.rootEl = document.createElement('div');
        this.rootEl.innerHTML = templateDiv;
        document.body.appendChild(this.rootEl);

        //
        this.questionGrid = null;

        //
        this.wordKeyEl = document.getElementById('word_key');

        // 计算
        let btnCalEl = document.getElementById('btn_cal');
        btnCalEl.addEventListener('click',()=>{
            let gameMap = G.gameMap;
            let grid = gameMap.getBoardData();
            if(!gameMap.isValidData(grid)){
                setting.alert('至少输入17个有效数字');
                return;
            }
            this.questionGrid = grid;
            try {
                const sudoku = new SudoKu(grid);
                if(sudoku.solve()){
                    let len = sudoku.results.length;
                    if(len > 1){
                        setting.alert('找到多个结果,只取第一个结果....');
                    }
                    let ret = sudoku.results[0];
                    console.log('结果为:', ret);
                    gameMap.fillResult(ret);
                }else{
                    setting.alert('计算结果失败....');
                }
            }catch (e) {
                setting.alert('发现异常,e:' + e.message);
            }

        },false);

        // 导出
        let btnExportEl = document.getElementById('btn_export');
        btnExportEl.addEventListener('click',()=>{

            // 1.检查  word_key
            let text = this.wordKeyEl.value.trim();
            if(text.length < 1){
                setting.alert('请输入 key.....');
                return;
            }

            if(!this.questionGrid){
                setting.alert('请先计算结果....');
                return;
            }

            // 2. 判断结果是否已经计算出来了
            let grid = G.gameMap.getBoardData();
            if(!this.isSolved(grid)){
                setting.alert('请先解数独');
                return;
            }

            // 导出保存
            let filePath = setting.getSaveFilePath('json');
            if(filePath){
                this.exportLevel(text,grid,filePath);
            }else{
                console.log('取消导出');
            }
        },false);

        // 清空
        let btnCleanEl = document.getElementById('btn_clean');
        btnCleanEl.addEventListener('click',this.btnCleanClicked.bind(this),false);
    }

    btnCleanClicked(){
        G.gameMap.clean();
    }

    isSolved(grid){
        for(let i = 0;i < grid.length;++i){
            for(let j = 0; j < grid[i].length;++j){
                if(grid[i][j] === 0){
                    return false;
                }
            }
        }
        return true;
    }

    exportLevel(text,grid,finalPath){
        let obj = {};
        obj.key = text;
        obj.q = this.questionGrid;
        obj.a = grid;

        window.fs.writeFileSync(finalPath,JSON.stringify(obj));
        setting.alert('保存成功');
    }
}

module.exports = EditorView;
